package com.tfswx.fljdmxyy.validation;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 不能为空字符串，但是可以为null
 *
 * @author ricky
 * @date 2021/11/11
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotEmptyStr.Validator.class)
public @interface NotEmptyStr {

    String message() default "{path}-不能为空字符串";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};


    class Validator implements ConstraintValidator<NotEmptyStr, Object> {

        @Override
        public boolean isValid(Object value, ConstraintValidatorContext context) {
            if (value == null) {
                return true;
            }
            boolean b = !"".equals(value.toString());
            if (!b) {
                ValidUtil.setDefaultParam(context);
            }
            return b;
        }
    }
}

